package hello.app.util;

import hello.app.model.HelloContactModel;
import hello.app.model.data.HelloContact;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;


public class Util {
	public static boolean isMobile(String number){
		return number.startsWith("090") || number.startsWith("080") || number.startsWith("070");
	}

	public static String suitableString(String str, int len){
		int end = str.length();
		String back = "";
		if (end > len) {
			end = len;
			back = "...";
		}
		return str.substring(0, end) + back;
	}

	public static String insertNewLine(String str, int interval){
		int len = str.length();
		StringBuilder sb = new StringBuilder(str);
		int count = 0;
		for(int i = interval; i < len+count-1; i+=interval){
			sb.insert(i+count, "\n");
			count++;
		}
		return sb.toString();
	}

	public static boolean isIntString(String number){
		try {
            Integer.parseInt(number);
            return true;
        } catch(NumberFormatException e) {
        	return false;
        }
	}

	/**
	 * HTMLタグ削除
	 *
	 * @param str 文字列
	 * @return HTMLタグ削除後の文字列
	 */
	public static String removeHtmlTag(String str) {
		// 文字列のすべてのタグを取り除く
		return str.replaceAll("<.+?>", "");
	}

	public static boolean isSet(String str){
		return str != null && str.length() != 0;
	}

	public static boolean isSet(Object[] objs){
		return objs != null && objs.length != 0;
	}

	public static int countChar(String str, char c){
		int count = 0;
		for(int i = 0; i < str.length(); i++){
			if(str.charAt(i) == c){
				count++;
			}
		}
		return count;
	}

	public static int getMaxLine(String str){
		String[] seps = str.split("\n");
		int max = 0;
		for(String s : seps){
			max = s.length() > max ? s.length() : max;
		}
		return max;
	}

	public static String join(String[] arry, String with) {
        StringBuffer buf = new StringBuffer();
        for (String s: arry) {
            if (buf.length()>0) {
                buf.append(with);
            }
            buf.append(s);
         }
         return buf.toString();
    }

	public static boolean isConnected(Context context){
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if( ni != null ){
            return cm.getActiveNetworkInfo().isConnected();
        }
        return false;
	}

	public static HelloContact protectContactName(HelloContactModel model, HelloContact newc){
		HelloContact oldc = model.findById(newc.getId());
		newc.setName(oldc.getName());
		return newc;
	}

	public static String getDeviceId(Context context){
		Account[] accounts = AccountManager.get(context).getAccounts();
		String device_id = "";
		for (Account account : accounts) {
			if (account.type.contains("com.google")) {
				device_id = account.name;
			}
		}

		if (device_id == "") {
			device_id = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
		}
		return device_id;
	}

	public static boolean isEmulator(Context context) {
	    return Settings.Secure.getString(context.getContentResolver(),
	                                Settings.Secure.ANDROID_ID) == null;
	}
}



